#!/usr/bin/env node

const { getAccessToken, urlOf, jsonOfResponse, config } = require('./index.js')
const https = require('https')

function createRepo( name, owner, is_org = false ) {
    console.log( "createRepo: " + name + " of " + owner )
    var url = urlOf( owner, is_org ) + "/repos" + "?access_token=" + getAccessToken() + "&private=true" + "&name=" + name + "&path=" + name

    console.log( "url: " + url )

    https.request( url, {
        method: "POST"
    }, ( res ) => {
        console.log( "" + res.statusCode )
        console.log( "" + res.statusMessage )
        jsonOfResponse( res, ( json ) => {
            console.log( "url: " + json.html_url )
            console.log( "ssh: " + json.ssh_url )
            console.log("git remote add origin " + json.html_url)
            console.log("git remote add origin " + json.ssh_url)
        } )
    } ).on( "error", ( error ) => {

    } ).end()
}

if (config.type == 'user') {
    createRepo(config.name)
}else if (config.type == 'orgs' ) {
    createRepo(config.owner, config.name, true)
}